﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using S3.DataModel;
using S3.Common;

namespace S3.Biz
{
    public class UserManager : CommonManager
    {
        public User GetOne(string userName)
        {
            try
            {
                return _entities.Users.FirstOrDefault(a => a.UserID == userName);
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("UserManager::GetOne::", ex, true);
                }
            }

            return null;
        }

        public bool Login(string userName, string password)
        {
            LogHelper log = new LogHelper();
            try
            {
                
                log.WriteLog("Called Biz Login");

                var ready = UserStatusValue.Text[UserStatus.Avaiable].ToLower();
                var list = _entities.Users.FirstOrDefault(a => a.UserID == userName && a.Password == password && a.Status.ToLower() == ready);
                if (list != null)
                {
                    log.WriteLog("Biz Login OK.");
                    return true;
                }

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    log.WriteLog(ex.Message);

                    exception.BizException("UserManager::Login::", ex, true);
                }
            }

            return false;
        }

        public bool ChangePass(string userName, string newPass)
        {
            var isSuccess = false;
            try
            {
                var user = _entities.Users.FirstOrDefault(a => a.UserID == userName);

                if (user != null)
                {
                    user.Password = newPass;
                    user.ModifiedBy = Constants.StaffName;
                    user.ModifiedDate = DateTime.Now;

                    isSuccess = true;
                }

                _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("UserManager::ChangePass::", ex, true);
                }
            }

            return isSuccess;
        }

        public bool Add(User input, bool isEdit)
        {
            var isSuccess = false;
            try
            {
                //Check deplicate                
                var user = _entities.Users.FirstOrDefault(a => a.UserID.ToLower() == input.UserID.ToLower());

                //If found
                if (user == null && !isEdit)
                {
                    input.Status = UserStatusValue.Text[UserStatus.Avaiable];
                    input.ModifiedBy = Constants.StaffName;
                    input.ModifiedDate = DateTime.Now;
                    _entities.AddToUsers(input);
                    _entities.SaveChanges();

                    isSuccess = true;
                }
                else if (isEdit && user != null)
                {
                    user.Status = UserStatusValue.Text[UserStatus.Avaiable];
                    user.ModifiedBy = Constants.StaffName;
                    user.ModifiedDate = DateTime.Now;
                    user.Role = input.Role;
                    user.Password = input.Password;
                    _entities.SaveChanges();

                    isSuccess = true;
                }

            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("UserManager::Add::", ex, true);
                }
            }

            return isSuccess;
        }

        public bool Delete(string userName)
        {
            var isSuccess = false;
            try
            {
                //Check deplicate
                var user = _entities.Users.FirstOrDefault(a => a.UserID.ToLower() == userName.ToLower());

                //If found
                if (user != null)
                {
                    _entities.DeleteObject(user);

                    isSuccess = true;
                }

                _entities.SaveChanges();
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("UserManager::Delete::", ex, true);
                }
            }

            return isSuccess;
        }

        public IQueryable<User> GetAll()
        {
            try
            {
                //Check deplicate
                return _entities.Users;
            }
            catch (Exception ex)
            {
                using (var exception = new SSDLoggingHandler())
                {
                    exception.BizException("UserManager::Delete::", ex, true);                                       
                }
            }

            return null;
        }
    }
}
